/*
*Author:Jackong
*Date:2012/07/11
*/
#include "StdAfx.h"
#include "cln_handler.h"

namespace fly {

	namespace jld{

		namespace client {	

			cln_handler::cln_handler(bool is_test)
				:cln_handler_(NULL)
				,target_(T_SELF)
				,is_test_(is_test)
			{
			}


			cln_handler::~cln_handler()
			{
				LOG_DEBUG("Delete cln_handler");
				delete cln_handler_;
				cln_handler_ = NULL;
			}
			cln_handler* cln_handler::set_next(cln_handler* cln_handler)
			{
				return cln_handler_ = cln_handler;
			}
			bool cln_handler::handle_send(recv_buf& recv_buf, send_buf& send_buf, cln_info& info)
			{
				if (NULL != cln_handler_)
				{
					return cln_handler_->handle_send(recv_buf, send_buf, info);
				}
				//LOG_WARN("Out of control of all send cln_handlers");
				return true;
			}

			bool cln_handler::handle_recv(recv_buf& recv_buf, send_buf& send_buf, cln_info& info)
			{
				if (ACK == recv_buf.pid())
				{
					Ack ack;
					recv_buf.parse_from_recv(ack);
					return handle_ack(ack, send_buf, info);
				}
				if (NULL != cln_handler_)
				{
					return cln_handler_->handle_recv(recv_buf, send_buf, info);
				}
				LOG_EMER("Out of control of all recv cln_handlers");
				return false;
			}

			bool cln_handler::handle_ack(Ack& ack, send_buf& send_buf, cln_info& info)
			{
				if (NULL != cln_handler_)
				{
					return cln_handler_->handle_ack(ack, send_buf, info); 
				}
				LOG_WARN("Out of control of all ack cln_handlers");
				return false;
			}
		}
	}
}